<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateProducts extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        if (Schema::hasTable('product')) {
            return;
        }

        // 品牌
        Schema::create('product_brand', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 64)->default('')->comment('品牌名称');
            $table->string('image', 255)->default('')->comment('品牌图片');
            $table->integer('sort_order')->default(0)->comment('排序');
            $table->unsignedTinyInteger('status')->default(1)->comment('状态');
            $table->integer('create_time')->nullable();
            $table->integer('update_time')->nullable();
        });

        // 配件
        Schema::create('product_bom', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('brand_id')->default(0)->comment('品牌ID');
            $table->string('name', 64)->default('')->comment('配件名称');
            $table->string('specification', 64)->default('')->comment('规格');
            $table->string('model', 64)->default('')->comment('型号');
            $table->string('uom', 32)->default('')->comment('计量单位');
            $table->integer('create_time')->nullable();
            $table->integer('update_time')->nullable();
        });

        // 类型配件
        Schema::create('product_type_bom', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('product_type_id')->default(0)->comment('产品类型ID');
            $table->unsignedInteger('product_bom_id')->default(0)->comment('配件ID');
            $table->unsignedInteger('count')->default(0)->comment('数量');
            $table->integer('create_time')->nullable();
            $table->integer('update_time')->nullable();
        });

        // 分类
        Schema::create('product_category', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('parent_id')->default(0)->comment('父级ID');
            $table->string('name', 64)->default('')->comment('分类名称');
            $table->integer('sort_order')->default(0)->comment('排序');
            $table->unsignedTinyInteger('status')->default(1)->comment('状态');
            $table->integer('create_time')->nullable();
            $table->integer('update_time')->nullable();
        });

        // 知识库
        Schema::create('knowledge', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title', 64)->default('')->comment('标题');
            $table->enum('type', ['published', 'unpublished', 'drafts'])->comment('类型，状态');
            $table->text('content')->comment('知识库详情');
            $table->integer('create_time')->nullable();
            $table->integer('update_time')->nullable();
        });

        // 知识库权限
        Schema::create('knowledge_permission', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('knowledge_id')->default(0)->comment('知识库ID');
            $table->unsignedInteger('role_id')->default(0)->comment('角色ID');
        });

        // 知识库标签
        Schema::create('knowledge_tag', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('knowledge_id')->default(0)->comment('知识库ID');
            $table->string('tag', 64)->default('')->comment('tag');
        });

        // 知识库附件
        Schema::create('knowledge_attachment', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('knowledge_id')->default(0)->comment('知识库ID');
            $table->string('attachment', 255)->default('')->comment('附件地址');
        });

        // 商品类型知识库
        Schema::create('product_type_knowledge', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('knowledge_id')->default(0)->comment('知识库ID');
            $table->unsignedInteger('product_type_id')->default(0)->comment('产品类型ID');
        });

        // 商品类型
        Schema::create('product_type', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('category_id')->default(0)->comment('分类ID');
            $table->string('category_name', 64)->default('')->comment('分类名称');
            $table->string('type_number', 64)->default('')->comment('类型编号');
            $table->string('name', 64)->default('')->comment('类型名称');
            $table->string('image', 255)->default('')->comment('类型图片');
            $table->text('intro')->comment('类型简介');
            $table->string('warranty_start_type')->default('')->comment('质保开始类型');
            $table->unsignedInteger('warranty_cycle_count')->default(0)->comment('质保周期时长');
            $table->string('warranty_start_unit')->default('')->comment('质保周期单位');
            $table->unsignedInteger('create_user_id')->default(0)->comment('操作人ID');
            $table->integer('create_time')->nullable();
            $table->integer('update_time')->nullable();
        });

        // 二维码
        Schema::create('product_qrcode', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('product_id')->default(0)->comment('商品ID');
            $table->integer('create_time')->nullable();
            $table->integer('update_time')->nullable();
        });

        // 商品目录
        Schema::create('product_menu', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('parent_id')->comment('父级ID');
            $table->string('name')->comment('目录名称');
            $table->integer('create_time')->nullable();
            $table->integer('update_time')->nullable();
        });

        // 商品
        Schema::create('product', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('customer_id')->default(0)->comment('客户ID');
            $table->unsignedInteger('type_id')->default(0)->comment('商品类型ID');
            $table->unsignedInteger('department_id')->default(0)->comment('服务部门ID');
            $table->string('product_number')->default('')->comment('产品编号');
            $table->string('name', 64)->default('')->comment('商品名称');
            $table->string('model', 64)->default('')->comment('商品型号');
            $table->string('attachment', 64)->default('')->comment('出厂报告地址');
            $table->unsignedInteger('country_id')->default(0)->comment('国家ID');
            $table->string('country_name', 64)->default('')->comment('国家名称');
            $table->unsignedInteger('province_id')->default(0)->comment('省份ID');
            $table->string('province_name', 64)->default('')->comment('省份名称');
            $table->unsignedInteger('city_id')->default(0)->comment('城市ID');
            $table->string('city_name', 64)->default('')->comment('城市名称');
            $table->unsignedInteger('county_id')->default(0)->comment('区县ID');
            $table->string('county_name', 64)->default('')->comment('区县名称');
            $table->string('address', 255)->default('')->comment('详细地址');
            $table->enum('warranty_status', ['in_warranty', 'out_warranty'])->comment('质保状态');
            $table->dateTime('warranty_start')->comment('质保开始时间');
            $table->dateTime('warranty_end')->comment('质保结束时间');
            $table->unsignedInteger('create_user_id')->default(0)->comment('操作人ID');
            $table->integer('create_time')->nullable();
            $table->integer('update_time')->nullable();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {

    }
}
